//
//  HTTruckLicenseView.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/24.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTTruckLicenseView.h"
#import "HTLicenseBoardView.h"

#define boardHeight kWidth(350)

@interface HTTruckLicenseView()<HTLicenseBoardViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong) HTLicenseBoardView *licenseBoardView;
@property (nonatomic,strong) UITextField *mainTextField;

@property (nonatomic,strong) UIView *backView;

@end

@implementation HTTruckLicenseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
       
        [self setupView];
    }
    return self;
}
- (void)setupView{
     self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
     self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kHTScreenWidth, (kHTScreenHeight - boardHeight))];
    self.backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [self addSubview:self.backView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
    [self.backView addGestureRecognizer:tap];
    [self addSubview:self.licenseBoardView];
    self.mainTextField = [[UITextField alloc]initWithFrame:CGRectMake(kWidth(40), (kHTScreenHeight - boardHeight)/2.0 - kWidth(35), kHTScreenWidth - kWidth(80), kWidth(80))];
    self.mainTextField.backgroundColor =[[UIColor blackColor]colorWithAlphaComponent:0.01];
    self.mainTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.backView addSubview:self.mainTextField];
    self.mainTextField.center = CGPointMake(self.mainTextField.center.x, self.backView.center.y);
    self.mainTextField.delegate = self;
    self.mainTextField.font = [UIFont systemFontOfSize:24];
    self.mainTextField.textColor = [UIColor whiteColor];
    self.mainTextField.textAlignment = NSTextAlignmentCenter;
    self.mainTextField.layer.borderWidth = 2.0f;
    self.mainTextField.layer.cornerRadius = kWidth(4);
    self.mainTextField.layer.borderColor = [UIColor whiteColor].CGColor;
    self.mainTextField.tintColor = [UIColor whiteColor];
    [self.mainTextField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    [self.licenseBoardView.cancleButton addTarget:self action:@selector(didCancle:) forControlEvents:UIControlEventTouchUpInside];
    [self.licenseBoardView.deleteButton addTarget:self action:@selector(didDelete:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)didCancle:(id)sender{
    [self dismissView];
}
- (void)didDelete:(id)sender{
    if (self.mainTextField.text == nil || self.mainTextField.text.length <= 0) {
        return;
    }
    self.mainTextField.text = [self.mainTextField.text substringToIndex:self.mainTextField.text.length - 1];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"text"])
    {
        NSString *text = [change valueForKey:NSKeyValueChangeNewKey];
        if (text != nil && text.length == 1) {
            _licenseBoardView.licenseLevel = LicenseBoardLevelTwo;
        }
        if (text != nil && text.length == 0) {
            _licenseBoardView.licenseLevel = LicenseBoardLevelOne;
        }
        if (text != nil && text.length == 7) {//车牌号
            if (self.delegate && [self.delegate respondsToSelector:@selector(didGotCallBackLicense:)]) {
                [self.delegate didGotCallBackLicense:text];
            }
            [self dismissView];
        }
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}
- (HTLicenseBoardView *)licenseBoardView{
    if (!_licenseBoardView) {
        _licenseBoardView = [[NSBundle mainBundle]loadNibNamed:@"HTLicenseBoardView" owner:self options:nil].firstObject;
        _licenseBoardView.frame = CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
        _licenseBoardView.delegate = self;
    }
    return _licenseBoardView;
}
- (void)didChoiceItemIndex:(NSInteger)index withTitle:(NSString *)title{
    
    self.mainTextField.text = [NSString stringWithFormat:@"%@%@",self.mainTextField.text,title];
    
}
-(void)showView{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        self.licenseBoardView.frame =  CGRectMake(0, kHTScreenHeight - boardHeight, kHTScreenWidth, boardHeight);
        [self layoutIfNeeded];
    }];
}
-(void)dismissView {
   
    [UIView animateWithDuration:.3 animations:^{
        self.licenseBoardView.frame =  CGRectMake(0, kHTScreenHeight, kHTScreenWidth, boardHeight);
        self.backView.alpha = 0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}
- (void)dealloc{
    [self.mainTextField removeObserver:self forKeyPath:@"text"];
}
@end
